﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CSDL_NC_07.DAO;
using System.Data.SqlClient;

namespace CSDL_NC_07.GUI.other
{
    public partial class MHThemHSNV : UserControl
    {
        public MHThemHSNV()
        {
            InitializeComponent();
        }

        private void MHThemHSNV_Load(object sender, EventArgs e)
        {
            //load ds bác sĩ lên combobox
            DataProvider dataProvider = new DataProvider();
            string sqlStr = "select bs.BacSi_ID, bs.TenBacSi from BACSI bs";
            if (!dataProvider.IsConnect())
                dataProvider.Connect();
            DataTable dt = dataProvider.ExcuteQueryDataTable(sqlStr);
            dataProvider.Disconnect();

            cbbBS.DataSource = dt;
            cbbBS.DisplayMember = "TenBacSi";
            cbbBS.ValueMember = "BacSi_ID";

            lblThongBao.Text = "";
            txtMaCTBA.Focus();
        }

        private void txtMaCTBA_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != ',' && e.KeyChar != '.' && e.KeyChar != (char)46 && e.KeyChar != (char)8)
            {
                if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                {
                    e.Handled = true;
                }
            }
        }

        private void txtMaCTBA_TextChanged(object sender, EventArgs e)
        {
            string _ctbaId = txtMaCTBA.Text;
            if (_ctbaId != "")
            {
                DataProvider dataProvider = new DataProvider();
                string sqlStr = "select top 1 ctba.BenhAn_ID from CHITIET_BENHAN ctba where ctba.CTBA_ID = " + _ctbaId;
                if (!dataProvider.IsConnect())
                    dataProvider.Connect();
                DataTable dt = dataProvider.ExcuteQueryDataTable(sqlStr);

                if (dt.Rows.Count == 1)
                {
                    sqlStr = "select top 1 ba.HSBN_ID from BENHAN ba where ba.BenhAn_ID = " + dt.Rows[0][0].ToString();
                    dt = dataProvider.ExcuteQueryDataTable(sqlStr);
                    sqlStr = "select top 1 * from HOSO_BENHNHAN hsbn where hsbn.HSBN_ID = " + dt.Rows[0][0].ToString();
                    dt = dataProvider.ExcuteQueryDataTable(sqlStr);
                    txtHoTen.Text = dt.Rows[0]["HoTen"].ToString();
                    txtCMND.Text = dt.Rows[0]["SoCMND"].ToString();
                    txtNgaySinh.Text = dt.Rows[0]["NgaySinh"].ToString();
                }
                else
                {
                    txtHoTen.Text = "";
                    txtCMND.Text = "";
                    txtNgaySinh.Text = "";
                }
                dataProvider.Disconnect();
            }
            else
            {
                txtHoTen.Text = "";
                txtCMND.Text = "";
                txtNgaySinh.Text = "";
            }
            lblThongBao.Text = "";
        }

        private void btnLapHS_Click(object sender, EventArgs e)
        {
            string _ctbaId = txtMaCTBA.Text;
            int _iCtbaId;
            if(_ctbaId=="")
            {
                lblThongBao.Text = "CHƯA NHẬP MÃ BỆNH ÁN";
                return;
            }

            try
            {
                _iCtbaId = int.Parse(_ctbaId);
            }
            catch 
            {
                lblThongBao.Text = "SAI ĐỊNH DẠNG MÃ BỆNH ÁN. VUI LÒNG NHẬP LẠI.";
                return;
            }

            DataProvider dataProvider = new DataProvider();
            if (!dataProvider.IsConnect())
                dataProvider.Connect();
            List<SqlParameter> parameters = new List<SqlParameter>();
            SqlParameter _paraCTBA = new SqlParameter("@CTBA_ID", SqlDbType.Int);
            _paraCTBA.Value = int.Parse(_ctbaId);
            parameters.Add(_paraCTBA);

            SqlParameter _paraBSDT = new SqlParameter("@BacSi_DieuTri", SqlDbType.Int);
            _paraBSDT.Value = int.Parse(cbbBS.SelectedValue.ToString());
            parameters.Add(_paraBSDT);

            SqlParameter _paraNgay = new SqlParameter("@NgayLap", SqlDbType.DateTime);
            _paraNgay.Value = DateTime.Now;
            parameters.Add(_paraNgay);

            SqlParameter _paraOut = new SqlParameter("@Out", SqlDbType.Int) { Direction = ParameterDirection.Output };
            parameters.Add(_paraOut);

            try
            {
                dataProvider.ExcuteProcedure("sp_ThemHoSoNhapVien", parameters.ToArray());
                if (int.Parse(_paraOut.Value.ToString()) == 1)
                {
                    MessageBox.Show("LẬP HỒ SƠ NHẬP VIỆN THÀNH CÔNG");
                    xoaForm();
                }
                else
                    MessageBox.Show("LỖI");
            }
            catch
            {
                MessageBox.Show("LỖI");
            }
        }

        private void xoaForm()
        {
            txtMaCTBA.Text = "";
            txtHoTen.Text = "";
            txtCMND.Text = "";
            txtNgaySinh.Text = "";
            cbbBS.SelectedItem = 0;
            lblThongBao.Text = "";
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            xoaForm();
        }
    }
}
